package com.little.g.springcloud.admin.web.controller.mall;

import com.little.g.springcloud.admin.web.annotation.RequiresPermissions;
import com.little.g.springcloud.admin.web.annotation.RequiresPermissionsDesc;
import com.little.g.springcloud.common.ResultJson;
import com.little.g.springcloud.common.utils.JacksonUtil;
import com.little.g.springcloud.common.utils.ResponseUtil;
import com.little.g.springcloud.mall.api.LitemallSystemConfigService;
import com.little.g.springcloud.mall.system.SystemConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api("商场配置")
@RestController
@RequestMapping("/admin/config")
@Validated
public class AdminConfigController {

	private final Log logger = LogFactory.getLog(AdminConfigController.class);

	@Reference
	private LitemallSystemConfigService systemConfigService;

	@ApiOperation("配置详情")
	@RequiresPermissions("admin:config:mall:list")
	@RequiresPermissionsDesc(menu = { "配置管理", "商场配置" }, button = "详情")
	@GetMapping("/mall")
	public ResultJson<Map<String, String>> listMall() {
		Map<String, String> data = systemConfigService.listMail();
		return ResponseUtil.ok(data);
	}

	@ApiOperation("配置修改")
	@RequiresPermissions("admin:config:mall:updateConfigs")
	@RequiresPermissionsDesc(menu = { "配置管理", "商场配置" }, button = "编辑")
	@PostMapping("/mall")
	public ResultJson updateMall(@RequestBody String body) {
		Map<String, String> data = JacksonUtil.toMap(body);
		systemConfigService.updateConfig(data);
		SystemConfig.updateConfigs(data);
		return ResponseUtil.ok();
	}

	@ApiOperation("运费配置")
	@RequiresPermissions("admin:config:express:list")
	@RequiresPermissionsDesc(menu = { "配置管理", "运费配置" }, button = "详情")
	@GetMapping("/express")
	public ResultJson<Map<String, String>> listExpress() {
		Map<String, String> data = systemConfigService.listExpress();
		return ResponseUtil.ok(data);
	}

	@ApiOperation("运费编辑")
	@RequiresPermissions("admin:config:express:updateConfigs")
	@RequiresPermissionsDesc(menu = { "配置管理", "运费配置" }, button = "编辑")
	@PostMapping("/express")
	public Object updateExpress(@RequestBody String body) {
		Map<String, String> data = JacksonUtil.toMap(body);
		systemConfigService.updateConfig(data);
		SystemConfig.updateConfigs(data);
		return ResponseUtil.ok();
	}

	@ApiOperation("订单配置")
	@RequiresPermissions("admin:config:order:list")
	@RequiresPermissionsDesc(menu = { "配置管理", "订单配置" }, button = "详情")
	@GetMapping("/order")
	public ResultJson<Map<String, String>> lisOrder() {
		Map<String, String> data = systemConfigService.listOrder();
		return ResponseUtil.ok(data);
	}

	@ApiOperation("更新订单配置")
	@RequiresPermissions("admin:config:order:updateConfigs")
	@RequiresPermissionsDesc(menu = { "配置管理", "订单配置" }, button = "编辑")
	@PostMapping("/order")
	public ResultJson updateOrder(@RequestBody String body) {
		Map<String, String> data = JacksonUtil.toMap(body);
		systemConfigService.updateConfig(data);
		return ResponseUtil.ok();
	}

	@ApiOperation("小程序配置")
	@RequiresPermissions("admin:config:wx:list")
	@RequiresPermissionsDesc(menu = { "配置管理", "小程序配置" }, button = "详情")
	@GetMapping("/wx")
	public ResultJson<Map<String, String>> listWx() {
		Map<String, String> data = systemConfigService.listWx();
		return ResponseUtil.ok(data);
	}

	@ApiOperation("修改小程序配置")
	@RequiresPermissions("admin:config:wx:updateConfigs")
	@RequiresPermissionsDesc(menu = { "配置管理", "小程序配置" }, button = "编辑")
	@PostMapping("/wx")
	public ResultJson updateWx(@RequestBody String body) {
		Map<String, String> data = JacksonUtil.toMap(body);
		systemConfigService.updateConfig(data);
		SystemConfig.updateConfigs(data);
		return ResponseUtil.ok();
	}

}
